package com.example.demo.controller.admin;

import com.example.demo.dao.Activity;
import com.example.demo.dao.ActivityExample;
import com.example.demo.dao.Goods;
import com.example.demo.service.GoodsService;
import com.github.pagehelper.Page;
import com.example.demo.service.activityService;
import com.example.demo.util.Msg;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/activity")
public class activityController {
    @Autowired
    private activityService activityService;
    @Autowired
    private GoodsService goodsService;
    //查找
    @GetMapping
    public String activityPage(){
        return "activity-list";
    }
    @GetMapping(value = "/search")
    @ResponseBody
    public Msg searchActivityResponse(@RequestParam("pageNum")Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestParam("keyword") String keyword){
        PageHelper.startPage(pageNum,pageSize);
        List<Activity> activities = activityService.listActivity(keyword);
        Msg msg = new Msg();
        return  msg.addActivity(((Page)activities).getTotal(),activities);
    }
    //添加
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Msg saveActivity(Activity activity){
            activityService.saveActivity(activity);
            return Msg.success("添加成功");
    }
    //单个删除
    @DeleteMapping("/delete")
    @ResponseBody
    public Msg delectActivity(@RequestParam("id") Integer id){
        activityService.delectActivity(id);
        return Msg.success("删除成功");
    }
    //批量删除
   @RequestMapping("/multiDel")
   @ResponseBody
    public Msg multiDel(@RequestParam("ids") String ids){
        activityService.multiDel(ids);
        return Msg.success("删除成功");
   }
    //获得选择的活动信息
    @GetMapping("/get")
    @ResponseBody
    public Activity getActivitypage(@RequestParam("id") Integer id){
        return activityService.findActivity(id);
    }

    //修改活动
    @PostMapping("/edit")
    @ResponseBody
    public Msg editActivity(Activity activity){
        activityService.editActivity(activity);
        return Msg.success("保存成功");
    }

    //获得所有活动信息
    @PostMapping("/show")
    @ResponseBody
    public Msg getActivity(){
        ActivityExample activityExample = new ActivityExample();
        activityExample.or();
        List<Activity> activities = activityService.getAllActivity(activityExample);
        return  Msg.success("获取活动信息成功").add("activities",activities);
    }
    //商品添加活动信息
    @PostMapping("/update")
    @ResponseBody
    public Msg updateGoodActivity(@RequestParam("goodsid") Integer goodsid,@RequestParam("activityid") Integer activityid){
        Goods goods = new Goods();
        goods.setGoodsid(goodsid);
        goods.setActivityid(activityid);
        goodsService.updateGoods(goods);
        return Msg.success("更新商品活动成功");
    }
}
